package com.foreknow.service.impl;

import com.foreknow.dao.impl.GuestBookDaoImpl;
import com.foreknow.db.DBManager;
import com.foreknow.model.GuestBook;
import com.foreknow.service.GuestBookService;
import com.foreknow.util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class GuestBookServiceImpl implements GuestBookService {
    private GuestBookDaoImpl gbDao = new GuestBookDaoImpl();
    private DBManager dbManager = DBManager.getInstance();
    private Connection conn = dbManager.getConnection();

    @Override
    public boolean gSave(GuestBook guestBook) {
        dbManager = DBManager.getInstance();
        conn = dbManager.getConnection();
        gbDao.setConnection(conn);
        boolean isRight = false;
        try {
            isRight = gbDao.gSave(guestBook);
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return isRight;
    }

    @Override
    public List<Object> gQuery() {
        dbManager = DBManager.getInstance();
        conn = dbManager.getConnection();
        gbDao.setConnection(conn);
        List<Object> list = new ArrayList<>();
        try {
            list =gbDao.gQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public boolean gUpdate(GuestBook guestBook, int id) {
        dbManager = DBManager.getInstance();
        conn = dbManager.getConnection();
        gbDao.setConnection(conn);
        boolean isRight = false;
        try {
            isRight = gbDao.gUpdate(guestBook, id);
            conn.commit();
            return isRight;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return isRight;
    }

    @Override
    public boolean gDelete(int id) {
        dbManager = DBManager.getInstance();
        conn = dbManager.getConnection();
        gbDao.setConnection(conn);
        boolean isRight = false;
        try {
           isRight = gbDao.gDelete(id);
           conn.commit();
           return isRight;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return isRight;
    }

    @Override
    public GuestBook gQueryById(int id) {
        dbManager = DBManager.getInstance();
        conn = dbManager.getConnection();
        gbDao.setConnection(conn);
        try {
            GuestBook gb = gbDao.gQueryById(id);
            conn.commit();
            return gb;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Pagination getByPage(int pageSize, int pageNumber) {
        dbManager = DBManager.getInstance();
        conn = dbManager.getConnection();
        gbDao.setConnection(conn);
        Pagination pagination = null;
        //调用持久层方法
        try {
            pagination= gbDao.getGuestBookByPage(pageSize, pageNumber);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return pagination;
    }


}
